package ui.utils
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import nsdecl.ns_bindattr_filter;
	import nsdecl.ns_dialog_service;
	import nsdecl.ns_drag_slot;
	import nsdecl.ns_group_filter;
	import nsdecl.ns_group_handler;
	import nsdecl.ns_group_hook;
	import nsdecl.ns_interactive_slot;
		
	use namespace ns_group_handler;
	use namespace ns_interactive_slot;
	use namespace ns_group_filter;
	use namespace ns_bindattr_filter;
	use namespace ns_group_hook;
	use namespace ns_drag_slot;
	use namespace ns_dialog_service;
	/**
	 * 运行时，寻找dobj所在的容器中，哪个容器具有fname这样的入口，并调用这个入口。
	 * @dobj - 从dobj开始往上层容器找对应的处理器
	 * @fname - 处理器的名字，需要保证这个函数是public的访问权限
	 * @errType - 如果出现异常，这个字符串会作为一个关键词来查找调试
	 * @dirType - 指引应该查找到的会是什么类型的实例
	 */
	public function dialogProperty( dobj:DisplayObject, fname:String, errType:String, dirType:Class=null ):*
	{		
		if( fname==null ) throw ArgumentError(errType + " Setter");
		
		var owner:DisplayObject = dobj;
		while( owner!=null )
		{
			if ( propertyIsAvailable(owner, fname) ) break;
			owner = owner.parent;
		}
		if( owner )
		{
			if( dirType==null )	return owner[ fname ];
			else if( owner[fname] is DisplayObjectContainer )
			{
				owner = owner[fname];
				for( var i:int=0; i < owner['numChildren']; ++i )
				{
					var child:DisplayObject = owner[ 'getChildAt' ](i);
					if( child is dirType ) return child;
					else if( child is PlaceSprite )
					{
						if( !child['hasGenerated'] ) child['generate']();
						if( child['component'] is dirType )
							return child['component'];
					}
				}
				return null;
			}
			return owner[fname];
		}
		else return null;
	}
}
import nsdecl.ns_bindattr_filter;
import nsdecl.ns_dialog_service;
import nsdecl.ns_drag_slot;
import nsdecl.ns_group_filter;
import nsdecl.ns_group_handler;
import nsdecl.ns_group_hook;
import nsdecl.ns_interactive_slot;

function propertyIsAvailable(object:Object, propertyName:Object):Boolean
{
	use namespace ns_group_hook;
	use namespace ns_group_handler;
	use namespace ns_group_filter;
	use namespace ns_interactive_slot;
	use namespace ns_bindattr_filter;
	use namespace ns_drag_slot;
	use namespace ns_dialog_service;
	
    var available:Boolean = false;
	if ( object.hasOwnProperty( propertyName ) )
		return true;

    try {
        var v:* = object[propertyName];
        available = (v!==undefined);
    } catch (e:Error) {
    }

    return available;
}